<?php

namespace App\V3\V3_1\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SellGoodsDetailModel extends Model
{
    use HasFactory;

    protected $table = 'sell_goods_detail';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    /**
     * $dates 数组内的列转换为日期格式
     *
     * @var array
     */
    protected $dates = [
        'sell_date'
    ];

    public function content()
    {
        return self::hasMany(MaterielProductContentModel::class, 'product_id', 'materiel_type_id');
    }
    static public function getGoodsDetailInfo($sellIdArr, $condition)
    {
        return self::select(
            'sell_id'
        )
            ->from('sell_goods_detail as a')
            ->leftJoin('materiel_type as b', 'a.materiel_type_id', '=', 'b.id')
            ->where($condition)
            ->whereIn('sell_id', $sellIdArr)
            ->distinct()
            ->get()->toArray();
    }

    public function goodsBatchInfo()
    {
        return self::hasMany(SellGoodsDetailBatchModel::class, 'sell_goods_detail_id', 'id');
    }

    static public function getBatchInfo($sellId)
    {
        return self::select(
            'materiel_type_id',
        )
            ->from('sell_goods_detail as a')
            ->leftJoin('sell_goods_detail_batch as b', 'a.id', '=', 'b.sell_goods_detail_id')
            ->where('a.sell_id', $sellId)
            ->get()->toArray();
    }

    static public function detail($sellId)
    {
        return self::select(
            'sell_goods_detail.id',
            'sell_goods_detail.id as sell_goods_id',
            'sell_goods_detail.materiel_type_id',
            'a.materiel_name as name',
            'a.type_name',
            'a.materiel_no',
            'a.materiel_img as image',
            'sell_goods_detail.display_ratio',
            'sell_goods_detail.calc_ratio',
            'sell_goods_detail.is_product_materiel',
            'price',
            'sell_goods_detail.num',
            'sell_goods_detail.out_num',
            'b.storage_name as warehouse',
            'b.id as warehouse_id',
            'piece',
            'sell_id',
            'cost_price',
            'sell_goods_detail.materiel_type_id',
            'sell_goods_detail.shelf_id',
            'sell_goods_detail.location_id',
            's.shelf_name',
            'l.location_name',
            'a.is_use_serial',
            'a.materiel_class_id',
            'a.materiel_unit',
            'a.is_product_materiel',
            'a.is_use_serial'
        )
            ->where('sell_id', $sellId)
            ->with('goodsBatchInfo', function($query) {
                $query->select(
                    'sell_goods_detail_batch.id',
                    'storage_batch_id',
                    'batch_no',
                    'a.storage_manage_id',
                    'sell_goods_detail_id',
                    'sell_id',
                    'a.main_id',
                    'sell_goods_detail_batch.num',
                    'rest_num'
                )
                    ->leftJoin('storage_batch as a', 'a.id', '=', 'sell_goods_detail_batch.storage_batch_id');
            })
            ->leftJoin('materiel_type as a', 'a.id', '=', 'sell_goods_detail.materiel_type_id')
            ->leftJoin('storage as b', 'b.id', '=', 'sell_goods_detail.warehouse_id')
            ->leftJoin('storage_shelf as s','sell_goods_detail.shelf_id','=','s.id')
            ->leftJoin('storage_shelf_location as l','sell_goods_detail.location_id','=','l.id')
            ->get()->toArray();
    }
}
